Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
@turf/length
Advanced tools
@turf/length is a module from the Turf.js library that allows you to calculate the length of a GeoJSON LineString or MultiLineString. It is useful for geographic data analysis and manipulation, particularly when working with spatial data in web applications.
Calculate Length of LineString
This feature allows you to calculate the length of a LineString in the specified units (e.g., miles, kilometers). The code sample demonstrates how to create a LineString and calculate its length in miles.
const turf = require('@turf/length');
const line = {
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[-77.031669, 38.878605],
[-77.029609, 38.881946],
[-77.020339, 38.884084],
[-77.025661, 38.885821]
]
}
};
const length = turf(line, {units: 'miles'});
console.log(length);
Calculate Length of MultiLineString
This feature allows you to calculate the length of a MultiLineString in the specified units (e.g., kilometers). The code sample demonstrates how to create a MultiLineString and calculate its length in kilometers.
const turf = require('@turf/length');
const multiLine = {
"type": "Feature",
"geometry": {
"type": "MultiLineString",
"coordinates": [
[
[-77.031669, 38.878605],
[-77.029609, 38.881946]
],
[
[-77.020339, 38.884084],
[-77.025661, 38.885821]
]
]
}
};
const length = turf(multiLine, {units: 'kilometers'});
console.log(length);
Geolib is a library for geospatial calculations, including distance and length calculations. It provides similar functionality to @turf/length but also includes additional features like calculating the distance between two points, finding the center of a collection of points, and more. It is more versatile but may be more complex to use for simple length calculations.
Geodesy is a library for geodesic calculations, including distance and length calculations. It offers similar functionality to @turf/length but focuses more on precise geodesic calculations, which can be useful for applications requiring high accuracy. It is more specialized and may be overkill for simple length calculations.
Takes a GeoJSON and measures its length in the specified units, (Multi)Point's distance are ignored.
geojson
Feature<(LineString | MultiLineString)> GeoJSON to measure
options
Object Optional parameters (optional, default {}
)
options.units
string can be degrees, radians, miles, or kilometers (optional, default kilometers
)var line = turf.lineString([[115, -32], [131, -22], [143, -25], [150, -34]]);
var length = turf.length(line, {units: 'miles'});
//addToMap
var addToMap = [line];
line.properties.distance = length;
Returns number length of GeoJSON
This module is part of the Turfjs project, an open source module collection dedicated to geographic algorithms. It is maintained in the Turfjs/turf repository, where you can create PRs and issues.
Install this single module individually:
$ npm install @turf/length
Or install the all-encompassing @turf/turf module that includes all modules as functions:
$ npm install @turf/turf
FAQs
turf length module
We found that @turf/length demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.